package cn.com.ddapp.ddm.ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.BrannlistAdpter;
import cn.com.ddapp.ddm.adapter.BrannlistProAdpter;
import cn.com.ddapp.ddm.adapter.PromotioAdpter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.BrannlistBean;
import cn.com.ddapp.ddm.bean.PromotioBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableGridView;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;
import okhttp3.Call;


/**
 * Created by Administrator on 2016/7/29 0029.
 * 天天特价
 */
public class PromotioActivity extends BaseActivity {
	@BindView(R.id.brand_refresh)
	PullToRefreshLayout brand_refresh;
	@BindView(R.id.brannlist_view)
    PullableListView brannlist_view;
	@BindView(R.id.empty)
	EmptyLayout emptyLayout;
	@BindView(R.id.img_error_layout)
	ImageView img_error_layout;
	private PromotioAdpter nAdapter;
//    private BrannlistProAdpter pAdapter;
	private List<PromotioBean.ProBean> nList;
	private int st=0;

	public static void startIntent(Context context){
		Intent intent=new Intent(context,PromotioActivity.class);
		context.startActivity(intent);
	}

	@Override
	public void initView() {

		setTitle("天天特价");
		setTitleLeftImg(R.mipmap.back_icon);
//        if(TDevice.isTablet()){
//            initLand();
//        }
//        else{
//        }
        initPro();

	}

    private void initPro() {
        requestProData(0);
        brand_refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                st=0;
                requestProData(0);
                brand_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                st=st+10;
                requestLoadData(st);
            }
        });
    }

    private void requestProData(final int i) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                PromotioBean bean = gson.fromJson(response, PromotioBean.class);
                if(!bean.getResult().equals("OK")){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }else if(bean.getPromotionList().size()==0){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }else{
                    nList=bean.getPromotionList();
                    nAdapter=new PromotioAdpter(PromotioActivity.this,nList);
                    brannlist_view.setAdapter(nAdapter);

                    brannlist_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent = new Intent(getApplicationContext(), CommidContentActivity.class);
                            int id = nList.get(i).getId();
                            intent.putExtra("type","id");
                            intent.putExtra("id", String.valueOf(id));
                            startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        String params="addrId="+ AppConfig.CITY_ID+"&st="+st;
        OKUtilAPI.post(OKUtilAPI.promotio, params, okUtilCallback);
    }
    private void requestLoadData(final int a) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback =new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                PromotioBean  bean = gson.fromJson(response, PromotioBean.class);
                List<PromotioBean.ProBean> ll=bean.getPromotionList();
                if(ll.size()>0){
                    st=a;
                    nAdapter.addView(ll);
                    nAdapter.notifyDataSetChanged();
                    brand_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                }else{
                    brand_refresh.loadmoreFinish(PullToRefreshLayout.NODATA);
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        String params="addrId="+ AppConfig.CITY_ID+"&st="+st;
        OKUtilAPI.post(OKUtilAPI.promotio, params, okUtilCallback);
    }
//    private void initLand() {
//        requestData(0);
//        brand_refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
//                st=0;
//                requestData(0);
//                brand_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
//            }
//
//            @Override
//            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
//                st=st+10;
//                requestLoadData(st);
//            }
//        });
//    }


    private void requestData(int i) {
//		emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
//		final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
//			@Override
//			public void onMyResponse(String response, int id) {
//                BrannlistBean bean = gson.fromJson(response, BrannlistBean.class);
//				if(!bean.getResult().equals("OK")){
//					emptyLayout.setErrorType(EmptyLayout.NODATA);
//				}else if(bean.getResult().equals("OK") && bean.getBrandList().size()==0){
//					emptyLayout.setErrorType(EmptyLayout.NODATA);
//				}else{
//					nList=bean.getBrandList();
//					nAdapter=new BrannlistAdpter(PromotioActivity.this,nList);
//                    brannlist_view.setAdapter(nAdapter);
//                    brannlist_view.setNumColumns(6);
//                    brannlist_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                        @Override
//                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                            Intent intent = new Intent(PromotioActivity.this, GoodClassifyActivity.class);
//                            intent.putExtra("classiftId",nList.get(i).getId());
//                            String lId=nList.get(i).getId()+"";
//                            SPUtils.setStringData(PromotioActivity.this,SPUtils.LID,lId);
//                            startActivity(intent);
//                        }
//                    });
//				}
//			}
//
//			@Override
//			public void onError(Call call, Exception e, int id) {
//				super.onError(call, e, id);
//				BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
//			}
//		};
//		String params="st="+st;
//		OKUtilAPI.post(OKUtilAPI.brand, params, okUtilCallback);
	}

	@Override
	public void initData() {

	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_promotio;
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}

	private void reequestData() {

	}


	@OnClick({R.id.img_error_layout})
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.img_error_layout:
                requestProData(0);
				break;


		}
	}

}
